<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Hash;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class HomeLuntanController extends Controller
{
    //主页模板
    public function getIndex()
    {
        //查询显示的分区信息
        $parts = DB::table('part')->where('status',2)->get();
        //查询显示的板块信息
        $sections = DB::table('section')->where('status',2)->get();
        //解析模板
        return view('Home.luntan.index',['parts'=>$parts,'sections'=>$sections]);
    }
    //列表页
    public function getLiebiao($id)
    {
    	//查询该模块内容
    	$section = DB::table('section')->where('id',$id)->first();
    	//查询所属分区的内容
    	$part = DB::table('part')->where('id',$section->pid)->first();
    	//查询该模块下帖子内容
    	$posts = DB::table('post')->where('sid',$id)->get();
    	//查询用户
    	$user = DB::table('user')->get();
    	// dd($user);
    	// dd($posts);
    	return view('Home.luntan.liebiao',['section'=>$section,'posts'=>$posts,'part'=>$part,'user'=>$user]);
    }
    //详情页
    public function getXiangqing($id)
    {
        // dd($id);
        $post = DB::table('post')->where('id',$id)->first();
        //查询对应模块内容
        $section = DB::table('section')->where('id',$post->sid)->first();
        //查询所属分区的内容
        $part = DB::table('part')->where('id',$section->pid)->first();
        //查询发此帖的用户
        $user = DB::table('user')->where('id',$post->uid)->first();
        // dd($post);
        return view('Home.luntan.xiangqing',['section'=>$section,'part'=>$part,'post'=>$post,'user'=>$user]);
    }
    //发帖页
    public function getPost($id)
    {
        //查询该模块内容
        $section = DB::table('section')->where('id',$id)->first();
        //查询所属分区的内容
        $part = DB::table('part')->where('id',$section->pid)->first();
        return view('Home.luntan.post',['section'=>$section,'part'=>$part]);
    }
    public function getPostadd(Request $request)
    {
        // dd($request->all());
        $arr = $request->only(['sid','title','content']);
        $arr['ptime'] = time();
        $arr['uid'] = session('id');
        // dd($arr);
        //添加并获取id号码
        $id = DB::table('post')->insertGetId($arr);
        //判断添加是否成功
        if($id > 0){
            return redirect('/home/luntan/xiangqing/'.$id)->with('success','帖子发表成功');
        }else{
            return back()->with('error','帖子发表失败');
        }
    }
}
